//
//  HWCommitCell.swift
//  HWanGame
//
//  Created by Apple on 2024/8/12.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class HWCommitCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20*widthScale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(11.font_medium).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium)
        .line(0).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_medium).alignment(.right).end()
    private let likeButton = c_make_ex(UIButton.self)
        .image("WuKong_like").image("Detail_like_sel", state: .selected).end()
    
    var likeCommitBlock: ((_ commitID: String) -> Void)?
    
    var commitModel: HWInfoCommentListModel? {
        didSet{
            guard let md = commitModel else { return }
            
            iconImageView.sd_setImage(with: URL(string: md.user?.face ?? ""))
            contentLabel.text = md.content
            nameLabel.text = md.user?.user_name
            timeLabel.text = md.time
            var commentCount = 0
            if let down = md.down?.count, down > 0 {
                commentCount = down
            }
            subLabel.text = "\(commentCount)评论 · " + md.zan + "点赞"
            likeButton.isSelected = md.is_zan == 1
        }
    }
    
    var model: HWGameDetailCommentListModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageView.sd_setImage(with: URL(string: md.touxiang))
            contentLabel.text = md.content
            nameLabel.text = md.user_name
            timeLabel.text = md.time
            subLabel.text = md.num + "评论 · " + md.zan + "点赞"
            likeButton.isSelected = md.is_zan
        }
        
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWGameCommentCell) {
        let reUse:String = "HWGameCommentCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameCommentCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameCommentCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        contentView.subviews{
            bgView.subviews{
                iconImageView
                nameLabel
                timeLabel
                contentLabel
                subLabel
                likeButton
            }
        }
        
        bgView.leading(12*widthScale).trailing(12*widthScale).top(0).bottom(12*widthScale)
        iconImageView.leading(12*widthScale).top(16*widthScale).size(40*widthScale)
        nameLabel.leading(64*widthScale).top(20*widthScale).trailing(15*widthScale)
        timeLabel.leading(64*widthScale).top(38*widthScale).trailing(15*widthScale)
        contentLabel.leading(64*widthScale).top(55*widthScale).trailing(16*widthScale)
        likeButton.trailing(8*widthScale).size(44*widthScale).bottom(0)
        likeButton.Top == contentLabel.Bottom
        subLabel.trailing(56*widthScale)
        subLabel.CenterY == likeButton.CenterY
        
        likeButton.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
    }
 
    @objc private func clickLikeButton(){
        
        if let ID = model?.id {
            likeCommitBlock?(ID)
        }
        if let ID = commitModel?.id {
            likeCommitBlock?(ID)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
//    @objc func buttonClick(_ button:UIButton) -> Void {
//        photoBroseView.imagesURL = model?.images
//        photoBroseView.frameFormWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.frameToWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.currentIndex = button.tag - 9999
//        photoBroseView.show()
//    }
//    
//    @objc func zanButtonClick(_ button:UIButton) -> Void {
//        self.zanButtonClick?(model?.id ?? "")
//    }
//    
//    @objc func videoButtonClick(_ button:UIButton) -> Void {
//        self.videoPlayClick?(self.model?.video ?? "")
//    }

}
